let username = localStorage.username
let contentBoxEl = document.querySelector('.contentBox')
let token = localStorage.token


void(async function isFav() {
    let address = await axios({
        method: 'post',
        url: 'http://localhost:9527/api/user/getAdd',
        data: {
            "username": username
        },
        headers: {
            'Authorization': 'Bearer ' + token
        }
    })
    let addData = address.data.data
    Object.keys(addData).forEach((e) => {
        console.log(addData[e]);
        let _id = addData[e]._id
        let realName = addData[e].realName
        let phone = addData[e].phoneNum
        let add = addData[e].userAdd
        let isDef = addData[e].isDef
        // console.log(isDef);
        let active = null
        if (isDef) {
            active = 'active'
        } else {
            active = null
        }

        let contentEl = document.createElement('ul')
        contentEl.classList.add('content')
        contentEl.dataset.id = _id
        contentEl.innerHTML = `
        <li class='usermsg'>
            <div>
                <img src="../../img/icon/home_light.png" alt="">
            </div>
            <ul>
                <li>
                    <span class="user">${realName}</span>
                    <span class="tel">${phone}</span>
                    <span class="labelBox active1">默认</span>
                </li>
                <li>${add}</li>
            </ul>
            <div class="modifyBtn">
                <img src="../../img/icon/edit.png" alt="">
            </div>
        </li>
        <li class='addressSet active1'>
            <div class="checkBox">
                <span class="checkBtn ${active}"></span>
                <span>设置默认地址</span>
            </div>
            <div class="deleteBox">
                <span class="deleteBtn">删除</span>
            </div>
        </li>
        `
        if (isDef) {
            contentBoxEl.prepend(contentEl)
        } else {
            contentBoxEl.append(contentEl)
        }

    })


    let checkBtnList = document.querySelectorAll('.checkBtn');
    let deleteList = document.querySelectorAll('.deleteBtn');
    let contentList = document.querySelectorAll('.content');
    let setBtn = document.querySelector('.setBtn');
    let addressList = document.querySelectorAll('.addressSet');
    let labelBox = document.querySelectorAll('.labelBox')
    let backBox = document.querySelector('.backBox')
    console.log(backBox);
    backBox.onclick = function() {
        console.log(111);
        window.history.back()
    }

    // 管理
    setBtn.onclick = function () {
        addressList.forEach(function (el) {
            el.classList.toggle('active1')
        })
    }
    // 小圆点
    checkBtnList.forEach(function (el, index) {
        el.index = index;
        el.onclick = async function () {
            let _id = el.closest('.content').dataset.id

            let outBox = el.closest('.content')
            contentBoxEl.prepend(outBox)

            let idx = this.index;
            let targetEl = checkBtnList[idx];
            checkBtnList.forEach(function (el) {
                el.classList.remove('active');
                // el.document.querySelector(label)
            });
            targetEl.classList.add('active');

            let res = await axios({
                method: 'post',
                url: 'http://localhost:9527/api/user/newAdd',
                data: {
                    "_id": _id
                }
            })
        }
    })
    // 删除
    deleteList.forEach(function (el, index) {
        el.index = index;
        el.onclick = function () {
            let idx = this.index;
            let targetEl = deleteList[idx];
            targetEl.closest('.content').remove()
        }
    })
    // 默认框

})()